<?php
declare (strict_types=1);

namespace app\common\middleware;

use think\facade\Request;
use think\Container;
class CheckValidate
{
    use \app\common\traits\JumpTrait;
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {

        if ($request->isPost()) {
            //获取当前参数
            $params = $request->param();


            if(!empty($params)){

                //拆解url
                $url = array_filter(explode('/',ltrim($request->request('s'),'/')));

                $module = $url[0];
                $controller = $url[1];
                $action = $url[2];


                if(count(explode('.',$controller)) <=1 ){
                    return $next($request);
                }

                //检查验证文件是否存在
                $validate = $this->exists_validate(explode('.',$controller), $module);
                if(empty($validate)){
                    return $next($request);
                }
                $results = $this->validate_data($action, $validate, $params);
                //dd($results);
                if($results){
                    return $this->error($results);
                }
            }
        }
        return $next($request);
    }

    /**
     * 验证数据
     * @param $action
     * @param $validate
     * @param $params
     * @return bool
     */
    public function validate_data($action, $validate, $params)
    {
        $scene = '';
        if(strstr($action,'add')){
            $scene = 'add';
        }
        if(strstr($action,'edit')){
            $scene = 'edit';
        }
        if(empty($scene)){
            return false;
        }
        $v = new $validate;
        if ($v->hasScene($scene)) {
            //设置当前验证场景
            $v->scene($scene);

            if (!$v->check($params)) {
                //校验不通过则直接返回错误信息
                return $v->getError();
            }else{
                return false;
            }
        }
    }

    /**
     * 检查文件是否存在
     * @param null $validate
     * @return bool|string
     */
    public function exists_validate($validate, $model)
    {


        //判断是否存在下划线
        if(count($validate) > 1 && strstr($validate[1],'_')){

            $validate[1] = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
                return strtoupper($matches[2]);
            }, $validate[1]);
            $mokuai = $model."\\";
            $file = "app\\".$mokuai."validate\\" .$validate[0]."\\". $validate[1];
        }else{
            $mokuai = $model."\\";
            $file = "app\\".$mokuai."validate\\" .$validate[0]."\\". $validate[1];
        }

        if(class_exists($file)){
            return $file;
        }
        return false;
    }

    /**
     * url 转数组
     * @param $data
     * @return array|null
     */
    public function url_to_array($data)
    {
        $arr = null;
        foreach ($data as $k=> $v){
            $arr = explode('/', $k);
            break;
        }
        return $arr;
    }

}